package com.zz.service.impl;

import com.zz.dto.OrderDTO;
import com.zz.entity.ShopOrder;
import com.zz.entity.ShopRecord;
import com.zz.repository.ShopOrderRepository;
import com.zz.repository.ShopRecordRepository;
import com.zz.service.OrderService;
import com.zz.util.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    ShopRecordRepository shopRecordRepository;
    @Resource
    ShopOrderRepository shopOrderRepository;
    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        Long orderId = KeyUtil.genUniqueLongKey();
        //1. 查询商品（数量, 价格）
        for (ShopRecord orderDetail: orderDTO.getOrderDetailList()) {
            //订单详情入库
            orderDetail.setShopRecordId(KeyUtil.genUniqueLongKey());
            orderDetail.setShopOrderId(orderId);
//            BeanUtils.copyProperties(productInfo, orderDetail);
            shopRecordRepository.save(orderDetail);
        }
        ShopOrder shopOrder=new ShopOrder();
        shopOrder.setShopOrderId(orderId);
        BeanUtils.copyProperties(orderDTO, shopOrder);
        shopOrderRepository.save(shopOrder);
        orderDTO.setShopOrderId(orderId);
        return orderDTO;
    }
}
